package fy.EAM.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;

import fy.EAM.bean.ComputerHandoverbean;
import fy.config.FyConfig;
import fy.mdm.dao.MaterialRequestNoTimerTask;
import fy.util.DBUtil;
public class ComputerHandoverdao {
	private static Log log = LogFactory.getLog(ComputerHandoverdao.class);
	private ComputerHandoverbean ComputerHandoverbean;
	private static String jobNum;
	private static String userName; 
	public boolean getMaintenance() {
//		System.err.println("数据进入");
		try {
			IServiceRequester Requester = BaseServiceRequester.getInstance();
			List<GroupRecord> grs1 = new ArrayList<GroupRecord>();//多层用
			// 定义请求报文对象
			IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
			String SourceSysID = "06004"; // 请求方(发布方)
			String ServiceID = "02012000000001";// 响应方(发布方)
			// 获取系统时间
			Date date = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			String format = simpleDateFormat.format(date);
			// 对请求报文对象进行设置
			reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
			reqMo.setSerialNO(format.substring(0, 8) + SourceSysID
					+ String.format("%06d", ++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
			reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
			reqMo.setServiceDateTime(format); // 设置报文 日期时间
			GroupRecord gr1 = new GroupRecord();
			gr1.setName("Asset");
			gr1.setFieldValue("AssetNumber",ComputerHandoverbean.getAssetNumber());
			gr1.setFieldValue("usingStartTime", ComputerHandoverbean.getUsingStartTime());
			gr1.setFieldValue("warrantyPeriod", ComputerHandoverbean.getWarrantyPeriod());
			gr1.setFieldValue("assetModel", ComputerHandoverbean.getAssetModel());
			gr1.setFieldValue("manufacturer", ComputerHandoverbean.getManufacturer());
			gr1.setFieldValue("SerialNumber", ComputerHandoverbean.getSerialNumber());
			gr1.setFieldValue("serviceId", ComputerHandoverbean.getServiceId());
			gr1.setFieldValue("allocation",ComputerHandoverbean.getAllocation());
			gr1.setFieldValue("displayer", ComputerHandoverbean.getDisplayer());
			gr1.setFieldValue("mouse", ComputerHandoverbean.getMouse());
			gr1.setFieldValue("keyBoard", ComputerHandoverbean.getKeyBoard());
			gr1.setFieldValue("powerAdapt", ComputerHandoverbean.getPowerAdapt());
			gr1.setFieldValue("macAddress", ComputerHandoverbean.getMacAddress());
			gr1.setFieldValue("wifiMac", ComputerHandoverbean.getWifiMac());
			User();
			gr1.setFieldValue("jobNum",jobNum);
			gr1.setFieldValue("userName",userName);
			gr1.setFieldValue("Description",ComputerHandoverbean.getDescription());
			gr1.setFieldValue("amount",ComputerHandoverbean.getAmount());
			gr1.setFieldValue("OABillINum",ComputerHandoverbean.getOABillINum());
			gr1.setFieldValue("cmpCode",ComputerHandoverbean.getCmpCode());
			gr1.setFieldValue("status","1");
			gr1.setFieldValue("htcIncredible",ComputerHandoverbean.getAssetName());
			gr1.setFieldValue("usingStartTime",ComputerHandoverbean.getUsingStartTime());
			
			grs1.add(gr1);
			reqMo.setReqGroupRecord(grs1);
			log.info("reqMo:"+reqMo);
			IMsgObject respMo = null;
			respMo = Requester.execute(reqMo);//发布报文
		} catch (Exception e) {
			// TODO: handle exception
		}
		return true;
		
	}

	private boolean  User(){
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;

		try {
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
			pst = conn.prepareStatement("select name,code from ORG_MEMBER where id=?");
			pst.setString(1,ComputerHandoverbean.getUserid());
			rs = pst.executeQuery();
			while (rs.next()) {
				userName=rs.getString(1);
				jobNum=rs.getString(2);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pst);
		}
		return true;
	}
	
	
	public ComputerHandoverbean getComputerHandoverbean() {
		return ComputerHandoverbean;
	}
	public void setComputerHandoverbean(ComputerHandoverbean computerHandoverbean) {
		ComputerHandoverbean = computerHandoverbean;
	}
}
